// pages/news/news.js
// 获取应用实例
const app = getApp()
Page({
    data: {
        hasTab: false, //是否存在tab分类
        pagepoint: "", //页面标识
        iPage: 1,
        iPageSize: 10,
        tabData: [], //分类数据
        listData: [], //列表数据
        listApi: "", //列表API名称
        listObj: {}, //列表接口参数
    },

    /**
     * 生命周期函数--监听页面加载
     * 公共列表页参数规则 详见配置档-新闻类型公共请求
     */
    onLoad(options) {
        var _this = this;
        var pagepoint = options.pagepoint;//页面标识
        if (pagepoint != null && pagepoint != undefined) { //获取页面标识信息
            var api_common_news = app.globalData.api_common_news;
            if (api_common_news.hasOwnProperty(pagepoint)) {
                _this.setData({ //更新页面标识
                    pagepoint: pagepoint,
                    pagename: api_common_news[pagepoint]['sTitle']
                })
                //检测是否存在tab
                if (api_common_news[pagepoint]['sTabApi'] != "" && api_common_news[pagepoint]['sTabApi'] != undefined) { //存在tab接口
                    //检测是否存在tab自定义参数
                    var tabObj = {};
                    if (api_common_news[pagepoint]['sTabPost'].length > 0) {
                        var sTabPost = api_common_news[pagepoint]['sTabPost'];
                        for (var i = 0; i < sTabPost.length; i++) {
                            if (options[sTabPost[i]] != null && options[sTabPost[i]] != undefined) {
                                tabObj[sTabPost[i]] = options[sTabPost[i]];
                            } else {
                                wx.$gcs_showToast("tab期待参数：" + sTabPost[i]);
                                return false;
                            }
                        }
                    }
                    //请求接口
                    wx.$requestApi(api_common_news[pagepoint]['sTabApi'], tabObj, function (res) {
                        if (res.code == 0) {
                            if (res.data.length > 0) {
                                _this.setData({
                                    hasTab: true,
                                    tabData: res.data
                                })
                            }
                        }
                    })
                }
                //检测列表数据
                if (api_common_news[pagepoint]['sListApi'] != "" && api_common_news[pagepoint]['sListApi'] != undefined) { //存在列表接口
                    //检测是否存在list自定义参数
                    var listObj = {};
                    listObj['iPage'] = _this.data.iPage;
                    listObj['iPages'] = 0;
                    listObj['iPageSize'] = _this.data.iPageSize;
                    //检测是否存在tab接口，若存在则iType不可为传递参数
                    if (api_common_news[pagepoint]['sTabApi'] != "" && api_common_news[pagepoint]['sTabApi'] != undefined) {
                        if (options.iType != null && options.iType != undefined) {
                            wx.$gcs_showToast("iType错误的传递");
                            return false;
                        } else { //存在tab接口，则默认iType为tab iID
                            listObj['iType'] = 0;
                        }
                    }
                    //检测是否存在list自定义参数
                    if (api_common_news[pagepoint]['sListPost'].length > 0) {
                        var sListPost = api_common_news[pagepoint]['sListPost'];
                        console.log(sListPost[0]);
                        for (var i = 0; i < sListPost.length; i++) {
                            if (options[sListPost[i]] != null && options[sListPost[i]] != undefined) {
                                listObj[sListPost[i]] = options[sListPost[i]];
                            } else {
                                wx.$gcs_showToast("列表接口期待参数：" + sListPost[i]);
                                return false;
                            }
                        }
                    }
                    _this.setData({
                        listApi: api_common_news[pagepoint]['sListApi'],
                        listObj: listObj
                    })
                    _this.getList();
                } else {
                    wx.$gcs_showToast("列表数据配置错误");
                }
            } else {
                wx.$gcs_showToast("不存在的页面标识");
            }
        } else {
            wx.$gcs_showToast("缺少页面标识");
        }
    },

    /**
     * gcs-news-tab,gcs-news-list组件事件处理
     * e.detail 属性获取
     * {id:1,type:'gcs-news-tab'}
     */
    gcsListener: function (e) {
        console.log(e.detail);
        var _this = this;
        if (e.detail.type == 'gcs-news-tab') { //tab被点击
            var listObj = _this.data.listObj;
            listObj.iPage = 1;
            listObj.iPages = 0;
            listObj.iType = e.detail.id;
            _this.setData({
                listObj: listObj,
                listData: [],
            })
            _this.getList();
        } else if (e.detail.type == 'gcs-news-list') { //新闻列表被点击
            wx.navigateTo({
                url: 'detail?pagepoint='+_this.data.pagepoint+'&id='+e.detail.id,
            })
        }
    },

    /**
     * 获取对应新闻信息
     */
    getList: function () {
        var _this = this;
        wx.$requestApi(_this.data.listApi, _this.data.listObj, function (res) {
            if (res.code == 0) {
                var listObj = _this.data.listObj;
                listObj.iPages = res.iPages;
                _this.setData({
                    listData: _this.data.listData.concat(res.aData),
                    listObj: listObj
                })
            } else {
                wx.$gcs_showToast(res.msg);
            }
        })
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        var _this = this;
        console.log(_this.data.iPage, _this.data.iPages);
        var listObj = _this.data.listObj;
        if (listObj.iPage < listObj.iPages) {
            listObj.iPage++;
            _this.setData({
                listObj: listObj
            })
            _this.getList();
        }
    }
})